package cn.edu.njpi.service.impl;

import cn.edu.njpi.mapper.TSchoolMapper;
import cn.edu.njpi.model.TSchool;
import cn.edu.njpi.service.TSchoolService;
import cn.edu.njpi.utils.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.service.impl
 * @date 2022/4/20 1:49 PM
 */
@Service
public class TSchoolServiceImpl implements TSchoolService {
    @Autowired
    private TSchoolMapper tSchoolMapper;

    @Override
    public List<TSchool> getAllSchool() {
        return tSchoolMapper.getAllSchool();
    }

    @Override
    public T addSchool(TSchool tSchool) {
        T t = new T();
        TSchool tSchool1 = tSchoolMapper.selectBySchoolName(tSchool.getSchoolName());
        if (tSchool1 == null) {
            tSchoolMapper.insertSelective(tSchool);
            t.add("school", tSchoolMapper.selectBySchoolName(tSchool.getSchoolName()));
            t.setStatus(200);
        } else {
            t.add("error", "该学校已存在");
            t.setStatus(503);
        }
        return t;
    }

    @Override
    public T deleteSchool(int schoolId) {
        T t = new T();
        try {
            tSchoolMapper.deleteByPrimaryKey(schoolId);
            t.setStatus(200);
        } catch (Exception e) {
            t.add("error", "未知错误");
            t.setStatus(500);
        }
        return t;
    }

    @Override
    public T changeSchool(TSchool tSchool) {
        T t = new T();
        TSchool tSchool1 = tSchoolMapper.selectBySchoolName(tSchool.getSchoolName());
        if (tSchool1 == null) {
            tSchoolMapper.updateByPrimaryKeySelective(tSchool);
            t.add("school", tSchoolMapper.selectByPrimaryKey(tSchool.getSchoolId()));
            t.setStatus(200);
        } else {
            t.add("error", "该学校已存在");
            t.setStatus(503);
        }
        return t;
    }

    @Override
    public TSchool selectTSchoolBySchoolId(Integer schoolId) {
        return tSchoolMapper.selectByPrimaryKey(schoolId);
    }
}
